package st.business.tepth.business.user.busnumber.workholder.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.jakewharton.rxbinding2.widget.RxTextView;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import st.business.tepth.R;
import st.business.tepth.business.bean.TargetObjBean;
import st.business.tepth.debug.DataProvider;
import st.business.tepth.view.ClearEditText;
import st.shadow.library.ui.recycler.BaseDecoration;

/**
 * @author zhangyu
 * @date 2019/2/18
 */
public class DialogTargetObjChoose extends Dialog {
    private OnItemChooseListener mChooseListener;

    public DialogTargetObjChoose(Context context, List<TargetObjBean> commonList,
                                 OnItemChooseListener listener) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mChooseListener = listener;
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_work_target, null);
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        RecyclerView recyclerViewCommon = view.findViewById(R.id.recycler_view_common_target);
        ClearEditText etSearch = view.findViewById(R.id.et_search);

        FlexboxLayoutManager commonLayoutManager = new FlexboxLayoutManager(getContext());
        commonLayoutManager.setFlexWrap(FlexWrap.WRAP);
        commonLayoutManager.setAlignItems(AlignItems.STRETCH);
        recyclerViewCommon.setLayoutManager(commonLayoutManager);
        recyclerViewCommon.setAdapter(new CommonTargetAdapter(R.layout.item_common_target_obj, commonList));
        recyclerViewCommon.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mChooseListener != null) {
                    mChooseListener.onItemChoose((TargetObjBean) adapter.getItem(position));
                }
                dismiss();
            }
        });

        final RecyclerView recyclerViewResult = view.findViewById(R.id.recycler_view_target_result);
        recyclerViewResult.setLayoutManager(new LinearLayoutManager(context));
        final SearchTargetAdapter searchTargetAdapter = new SearchTargetAdapter(R.layout.item_target_search_result, null);
        searchTargetAdapter.setEmptyView(R.layout.view_search_empty,
                (ViewGroup) recyclerViewResult.getParent());
        recyclerViewResult.setAdapter(searchTargetAdapter);
        recyclerViewResult.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mChooseListener != null) {
                    mChooseListener.onItemChoose((TargetObjBean) adapter.getItem(position));
                }
                dismiss();
            }
        });
        recyclerViewResult.addItemDecoration(BaseDecoration.
                create(Color.parseColor("#f0f0f0"), 1));
        Observer observable = RxTextView.textChanges(etSearch)
                .subscribeOn(AndroidSchedulers.mainThread())
                .debounce(600, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
                .filter(new Predicate<CharSequence>() {
                    @Override
                    public boolean test(CharSequence charSequence) throws Exception {
                        if (charSequence.length() == 0) {
                            recyclerViewResult.setVisibility(View.GONE);
                        } else {
                            recyclerViewResult.setVisibility(View.VISIBLE);
                        }
                        return charSequence.length() > 0;
                    }
                })
                .switchMap(new Function<CharSequence, Observable<List<TargetObjBean>>>() {
                    @Override
                    public Observable<List<TargetObjBean>> apply(CharSequence charSequence) throws Exception {
                        return Observable.create(new ObservableOnSubscribe<List<TargetObjBean>>() {
                            @Override
                            public void subscribe(ObservableEmitter<List<TargetObjBean>> emitter) throws Exception {
                                emitter.onNext(DataProvider.getTargetObjList());
                            }
                        });
                    }
                })
                .subscribeWith(new Observer<List<TargetObjBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<TargetObjBean> response) {
                        // recyclerViewResult.setVisibility(View.VISIBLE);
                        searchTargetAdapter.setNewData(response);
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
        setContentView(view);
    }

    private class CommonTargetAdapter extends BaseQuickAdapter<TargetObjBean, BaseViewHolder> {

        CommonTargetAdapter(int layoutResId, @Nullable List<TargetObjBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, TargetObjBean item) {
            helper.setText(R.id.tv_bus_number, item.getName());
        }
    }

    private class SearchTargetAdapter extends BaseQuickAdapter<TargetObjBean, BaseViewHolder> {

        SearchTargetAdapter(int layoutResId, @Nullable List<TargetObjBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, TargetObjBean item) {
            helper.setText(R.id.tv_target, item.getName());
        }
    }

    public interface OnItemChooseListener {
        void onItemChoose(TargetObjBean targetObjBean);
    }
}
